<?php
namespace app\admin\controller;
use app\common\model\Manager as ManagerModel;
use app\common\model\ManagerStudent as ManagerStudentModel;
use app\common\model\Student;
use app\common\model\Manager;
use app\common\model\ManagerGongzi;
use app\common\model\Config;

class Managerstudent extends Base
{
    public function __construct () {
        parent::__construct();
    }
    /*
     列表
     */
    public function list(){
        $input = input('');
        $where = array(
            'mid' => $input['mid'],
            'time' => $input['time']
        );
        if($input['sid']){
            $where['sid'] = $input['sid'];
        }
        $list = ManagerStudentModel::getAll($where);
        if($list){
            $sids = ManagerStudentModel::getColumn($where,'sid');
            $student = Student::getColumn(array('id'=>array('in',$sids)),'id,name');
            foreach ($list as $key => $value){
                if($input['name']){
                    if(strchr($student[$value['sid']],$input['name'])){

                        $list[$key]['name'] = $student[$value['sid']];
                    }else{
                        unset($list[$key]);
                    }
                }else{
                    $list[$key]['name'] = $student[$value['sid']];
                }

            }
        }
        return returnJson(0,'',$list?$list:[]);
    }

    // 添加招新学员
    public function add()
    {
        if(request()->isPost()){
            $input  = input('post.');
            $where = array();
            // 学员信息是否存在
            if ($input['sid']){
                // 获取学员信息
                $s_where['id'] = $input['sid'];
                $student = Student::getColumn($s_where,'id');
                // 学员信息是否存在
                if(!$student){
                    return returnJson(1,'该学员不存在');
                }else{
                    //本月是否已经被其他教练招新续费
                    $m_s_where = array(
                        'sid' => $input['sid'],
                        'time' => $input['time']
                    );
                    $m_s = ManagerStudentModel::getOne($m_s_where);
                    //管理员信息
                    $m = Manager::getColumn(array(),'id,name');
                    if($m_s){
                        return returnJson(1,"该学员本月招新已经记录在了 {$m[$m_s['mid']]} 教练名下");
                    }else{
                        // 添加记录
                        $time = time();
                        $save = array(
                            'sid' => $input['sid'],
                            'time' => $input['time'],
                            'mid' => $input['mid'],
                            'update_time' => $time,
                            'create_time' => $time
                        );
                        ManagerStudentModel::setInsertGetId($save);
                        // 更新工资
                        $where['id']=$input['id'];
                        $config = Config::getColumn(array(),'name,value');
                        ManagerGongzi::setFieldInc($where,'zhaoxin',$config['zhaoxin_gongzi']);
                        return returnJson(0,'添加成功');
                    }
                }
            }else{
                return returnJson(1,'请填写学员ID');
            }
        }

        return returnJson(1,'添加失败');
    }

    //删除招新信息
    public function del()
    {
        if(request()->isPost()){
            $input = input('post.');
            $where = array(
                'id' => $input['id']
            );
            ManagerStudentModel::setDel($where);
            $g_where = array(
                'id' => $input['gid']
            );
            $config = Config::getColumn(array(),'name,value');
            ManagerGongzi::setFieldDec($g_where,'zhaoxin',$config['zhaoxin_gongzi']);
            return returnJson(0,'删除成功');
        }
        return returnJson(1,'删除失败');
    }

}
